package com.qipay.admin.cp.controller;

import com.qipay.admin.BaseController;
import com.qipay.admin.login.AuthPassport;
import com.qipay.admin.withdraw.service.GoogleAuthenticator;
import com.qipay.baas.model.CpWithdrawTradlog;
import com.qipay.baas.model.CpWithdrawTradlogExample;
import com.qipay.baas.model.WhiteIps;
import com.qipay.core.entity.Result;
import com.qipay.core.help.ResultHelper;
import com.qipay.ips.service.IpsService;
import com.qipay.mapper.CpWithdrawTradlogMapper;
import com.qipay.security.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/cp")
public class GgsController extends BaseController {

    @Autowired
    IpsService ipsService;

    @Autowired
    CpWithdrawTradlogMapper cpWithdrawTradlogMapper;

    @AuthPassport(key = "admin")
    @RequestMapping(value = {"/chongzhi"}, method = RequestMethod.POST)
    @ResponseBody
    public Result<String> channelTypeList(@AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser,
                                          @RequestParam(value = "cpId", required = true) Long cpId,
                                          @RequestParam(value = "money", required = true) Double money,
                                          @RequestParam(value = "remark", required = true) String remark,
                                          @RequestParam(value = "code", required = true) String code
    ) {
        //先校验验证码
        List<WhiteIps> whiteIpsList = ipsService.query(loginUser);
        if (whiteIpsList.size()>0){
            String secret=whiteIpsList.get(0).getIpAddress();

            long code1 = Long.valueOf(code);
            long t = System.currentTimeMillis();
            GoogleAuthenticator ga = new GoogleAuthenticator();
            ga.setWindowSize(1);
            boolean r = ga.check_code(secret, code1, t);
            if (!r){//验证不成功
                return ResultHelper.error("验证码错误");
            }
        }
        long amount = new BigDecimal(money).multiply(new BigDecimal("100")).longValue();
        //充值
        CpWithdrawTradlog cpWithdrawTradlog = new CpWithdrawTradlog();
        cpWithdrawTradlog.setAmount(amount*-1);//这是提现列表，为负，表示增加
        cpWithdrawTradlog.setCreateTime(new Date());
        cpWithdrawTradlog.setOperation((byte)1);
        cpWithdrawTradlog.setCpId(cpId);
        cpWithdrawTradlog.setState((byte)0);
        cpWithdrawTradlog.setRemark(remark);
        cpWithdrawTradlog.setType((byte)0);
        cpWithdrawTradlog.setFee(new BigDecimal("0"));
        cpWithdrawTradlog.setActualAmount(amount*-1);
        cpWithdrawTradlog.setFinishTime(new Date());
        cpWithdrawTradlog.setAttach(loginUser.getUsername());

        cpWithdrawTradlogMapper.insert(cpWithdrawTradlog);

        return ResultHelper.success("添加成功");
    }

    @AuthPassport(key = "admin")
    @RequestMapping(value = {"/getJiajian"}, method = RequestMethod.GET)
    @ResponseBody
    public Result<String> getJiajian(@AuthenticationPrincipal UserDetailsServiceImpl.LoginUserDetails loginUser,
                                          @RequestParam(value = "cpId", required = true) Long cpId
    ) {
        CpWithdrawTradlogExample cpWithdrawTradlogExample = new CpWithdrawTradlogExample();
        cpWithdrawTradlogExample.createCriteria().andCpIdEqualTo(cpId).andOperationEqualTo((byte)1);
        cpWithdrawTradlogExample.setOrderByClause("id DESC");
        List<CpWithdrawTradlog>cpWithdrawTradlogList = cpWithdrawTradlogMapper.selectByExample(cpWithdrawTradlogExample);
        return ResultHelper.success("添加成功",cpWithdrawTradlogList);
    }

}
